package com.ruoyi.partybuilding.common;

import cn.hutool.core.bean.BeanUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.var;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.stream.Collectors;

public class DataUtil {
    /** 从list中查找对应select对象，然后赋值到list对象
     *
     * @param list
     * @param select
     * @param where
     * @param predicate
     * @param <T>
     * @param <K>
     */
    public static  <T,K> void findSet(Collection<T> list, Collection<K> select, BiFunction<T,K, Boolean> where, BiConsumer<T,K> predicate) {
        list.forEach(p -> {
            var tmp = select.stream().filter(x -> where.apply(p, x)).findFirst();
            tmp.ifPresent(z -> predicate.accept(p, tmp.get()));
        });
    }
    public static  <T,K,V> List<V> findSetRe(Collection<T> list, Collection<K> select, BiFunction<T,K, Boolean> where, BiFunction<T,K,V> predicate) {
        var re = new ArrayList<V>();
        list.forEach(p -> {
            var tmp = select.stream().filter(x -> where.apply(p, x)).findFirst();
            if(tmp.isPresent())
                re.add(predicate.apply(p, tmp.get()));
            else
                re.add(predicate.apply(p, null));
        });
        return re;
    }
    public static Map toMap(Object fromValue, Consumer<Map> predicate) {
        var map = new ObjectMapper().convertValue(fromValue, Map.class);
        if (predicate != null)
            predicate.accept(map);
        return map;
    }
    public static List<Map> toMapList(Collection<?> fromValue, Consumer<Map> predicate) {
        return fromValue.stream().map(p -> toMap(p, predicate)).collect(Collectors.toList());
    }
    public  static  <T> T copyProperties(Object source, T target){
        BeanUtils.copyProperties(source,target);
        return target;
    }
}
